package com.elitel.frame.business.controller;

import com.elitel.frame.base.service.CfgDbService;
import com.elitel.frame.base.service.SysPrjServiceService;
import com.elitel.frame.base.service.SysReqLogService;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @author mulis
 */
@RestController
@RequestMapping({"/system/statistics", "/sys/statistics/"})
public class SysServerStatisticsController {
    private static final Logger logger = LoggerFactory.getLogger(SysServerStatisticsController.class);

    @Resource
    private SysReqLogService sysReqLogService;

    @Resource
    private SysPrjServiceService sysPrjServiceService;
    @Resource
    private CfgDbService cfgDbService;


    @RequestMapping("/serverStatList/{type}")
    @ResponseBody
    public JsonResult serverStatList(@PathVariable("type") String type) {
        JsonResult res = new JsonResult();
        List<List<String>> lists = sysReqLogService.selectTopByDays(7, type);
        res.setSuccess(true);
        res.setData(lists);
        return res;
    }

    @RequestMapping("/recentDateList")
    @ResponseBody
    @Deprecated
    public JsonResult recentDateList(@RequestParam Map<String, String> params) {
        JsonResult res = new JsonResult();
        try {
            // 默认统计前七天和前10名的服务
            Integer days = StringUtils.isEmpty(params.get("days")) ? 7 : Integer.parseInt(params.get("days"));
            Map<String, Object> maps = sysReqLogService.recentDateList(days);
            res.setSuccess(true);
            res.setData(maps);
        } catch (Exception e) {
            logger.error("查询服务调用次数异常", e);
            res.setSuccess(false);
            res.setMsg("查询服务调用次数异常");
        }
        return res;
    }

    @RequestMapping("/serviceTypeList")
    @ResponseBody
    public JsonResult serviceTypeList(@RequestParam Map<String, String> params) {
        JsonResult res = new JsonResult();
        try {
            List<Map<String, Object>> maps = sysPrjServiceService.selectCountGroupByType();
            res.setSuccess(true);
            res.setData(maps);
        } catch (Exception e) {
            logger.error("查询服务调用次数异常", e);
            res.setSuccess(false);
            res.setMsg("查询服务调用次数异常");
        }
        return res;
    }

    @RequestMapping("/invokeRecordList")
    @ResponseBody
    public JsonResult invokeRecordList(@RequestParam Map<String, String> params) {
        JsonResult res = new JsonResult();
        try {
            // 页码
            int pageNO = StringUtils.isEmpty(params.get("pageNO")) ? 1 : Integer.parseInt(params.get("pageNO"));
            pageNO = Math.max(pageNO, 1);
            // 页数
            int pageSize = StringUtils.isEmpty(params.get("pageSize")) ? 10 : Integer.parseInt(params.get("pageSize"));
            pageSize = pageSize < 1 ? 10 : pageSize;
            // 分页查询 这里是整型 不考虑精度丢失
            List<Map<String, Object>> maps = sysReqLogService.invokeRecordList(pageNO, pageSize, params.get("status"));
            res.setSuccess(true);
            res.setData(maps);
        } catch (Exception e) {
            logger.error("查询服务调用次数异常", e);
            res.setSuccess(false);
            res.setMsg("查询服务调用次数异常");
        }
        return res;
    }

    @RequestMapping("/serverStat")
    @ResponseBody
    public JsonResult serverStat(@RequestParam Map<String, String> params) {
        JsonResult jsonResult = new JsonResult();
        ServerStatVO serverStatVO = sysReqLogService.serverStat(params);
        jsonResult.setSuccess(true);
        jsonResult.setData(serverStatVO);
        return jsonResult;
    }

    @RequestMapping("/statisticalAnalysisTables")
    @ResponseBody
    public JsonResult statisticalAnalysisTables() {
        JsonResult jsonResult = new JsonResult();
        StatAnalTableVO statAnalTableVO = sysReqLogService.statisticalAnalysisTables();
        jsonResult.setSuccess(true);
        jsonResult.setData(statAnalTableVO);
        return jsonResult;
    }

    @RequestMapping("/statisticalAnalysisServices")
    @ResponseBody
    public JsonResult statisticalAnalysisServices() {
        JsonResult jsonResult = new JsonResult();
        StatAnalServicesVO statAnalServicesVO = sysReqLogService.statisticalAnalysisServices();
        jsonResult.setSuccess(true);
        jsonResult.setData(statAnalServicesVO);
        return jsonResult;
    }

    /**
     * 数据源状态
     *
     * @return
     */
    @RequestMapping("/dataSourceStatus")
    @ResponseBody
    public JsonResult dataSourceStatus(PageInfo pageInfo) {
        JsonResult res = new JsonResult();
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            // 分页查询 这里是整型 不考虑精度丢失
            List<CfgDb> vo = cfgDbService.dataSourceStatus();
            res.setSuccess(true);
            res.setData(vo);
        } catch (Exception e) {
            logger.error("查询服务调用次数异常", e);
            res.setSuccess(false);
            res.setMsg("查询服务调用次数异常");
        }
        return res;
    }

    /**
     * 任务执行情况
     *
     * @return
     */
    @RequestMapping("/jobExecuteCondition")
    @ResponseBody
    public JsonResult jobExecuteCondition(PageInfo pageInfo) {
        JsonResult res = new JsonResult();
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            // 分页查询 这里是整型 不考虑精度丢失
            List<JobExecuteConditionVo> vo = sysReqLogService.jobExecuteCondition();
            res.setSuccess(true);
            res.setData(vo);
        } catch (Exception e) {
            logger.error("查询服务调用次数异常", e);
            res.setSuccess(false);
            res.setMsg("查询服务调用次数异常");
        }
        return res;
    }

}
